//
//  GYZSearchViewController.m
//  Qyer
//
//  Created by GYz on 15/10/23.
//  Copyright © 2015年 GF. All rights reserved.
//

#import "GYZSearchViewController.h"
#import "BaseHeader.h"
@interface GYZSearchViewController () <UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong)UISearchBar *search;

@end

@implementation GYZSearchViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatNav];
}

- (void)creatNav
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    
    self.search = [[UISearchBar alloc]initWithFrame:CGRectMake(30, 25, SCREEN_WIDTH - 110, 30)];

    self.search.barTintColor = [UIColor whiteColor];
    self.search.layer.borderColor = [[UIColor grayColor]CGColor];
    self.search.layer.borderWidth = .7;
    self.search.layer.masksToBounds = YES;
    self.search.layer.cornerRadius = 13.;
    self.search.delegate = self;
    [view addSubview:self.search];
    [self.search becomeFirstResponder];
    
    UIButton *button = [self creatButtonWithRect:CGRectMake(SCREEN_WIDTH - 65, 25, 50, 30) imageStr:nil click:@selector(cancelClick:)];
    [button setTitle:@"取消" forState:UIControlStateNormal];
    [button setTitleColor:kCOLOR(41, 195, 51) forState:UIControlStateNormal];
    [button.titleLabel setFont:kFont(15.0)];
    [view addSubview:button];
    
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(10, 63, SCREEN_WIDTH - 10, 1)];
    line.backgroundColor = [UIColor lightGrayColor];
    [view addSubview:line];
}

- (void)cancelClick:(UIButton *)sender
{
    [self.search resignFirstResponder];
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - searchBarDelegate
// 实时搜索
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    [self afn];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
}

#pragma mark - 网络请求
- (void)afn
{
    self.infoArr = [[NSMutableArray alloc] init];
    if (_search.text != nil) {
        [AFN getdataWithString:[NSString stringWithFormat:@"http://open.qyer.com/qyer/search/index?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&keyword=%@&oauth_token=d5ecb9febb61510d54ba2d47d986cf65&page=1&place_field=country|city|poi&track_app_version=6.6.6&track_deviceid=301FBCE0-A789-4EAC-ADF1-95E1AACF2A70&track_user_id=6695170&type=place&v=1", _search.text] block:^(id block) {
           
            NSDictionary *dataDic = [block objectForKey:@"data"];
            for (NSDictionary *dic in [dataDic objectForKey:@"entry"]) {
                GYZSearchModel *model = [GYZSearchModel modelWithDictionary:dic];
                [self.infoArr addObject:model];
            }
            
            if (self.tableView == nil) {
                [self creatTableView];
            }
            [self.tableView reloadData];
            
        } BLOCK:^(id BLOCK) {
        }];
    }
}

#pragma mark - 创建tableView
- (void)creatTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview: self.tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.infoArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"search";
    GYZSearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[GYZSearchTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    GYZSearchModel *model = self.infoArr[indexPath.row];
    [cell setValueWith:model];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return SCREEN_WIDTH / 4 + 20;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    GYZSearchModel *model = self.infoArr[indexPath.row];
    if ([model.label isEqualToString:@"国家"]) {
        GYZCountryViewController *country = [[GYZCountryViewController alloc] init];
        country.cid = model.cid;
        [self.navigationController pushViewController:country animated:YES];
    }
    if ([model.label isEqualToString:@"城市"]) {
        GYZCityViewController *city = [[GYZCityViewController alloc] init];
        city.cid = model.cid;
        [self.navigationController pushViewController:city animated:YES];
    }
    // 美食 http://open.qyer.com/qyer/footprint/poi_detail?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.88273567151845&lon=121.5397659200424&oauth_token=aa3e2c9cfa1fa80cc75956eeb20534b3&page=1&poi_id=35523&screensize=640&track_app_channel=App%2520Store&track_app_version=6.7&track_device_info=iPhone%25205s&track_deviceid=DD5CD9DA-15F0-4236-B9DF-8B587C98B73A&track_os=ios%25209.1&track_user_id=6899839&v=1
    
    // 购物 http://open.qyer.com/qyer/footprint/poi_detail?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.88273567151845&lon=121.5397659200424&oauth_token=aa3e2c9cfa1fa80cc75956eeb20534b3&page=1&poi_id=122678&screensize=640&track_app_channel=App%2520Store&track_app_version=6.7&track_device_info=iPhone%25205s&track_deviceid=DD5CD9DA-15F0-4236-B9DF-8B587C98B73A&track_os=ios%25209.1&track_user_id=6899839&v=1
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
